<!--#include file="functions.asp" -->
<%

'set cn=Server.CreateObject("ADODB.Connection")
'cn.Open "MP2ENTTEST2", "MP2", "MP2"
dim sql

if (request("seqnum2")="0") then
  dim InvRegNo  
  sql = "SELECT REGPREFIX, REGNUM FROM EXJRNLPREF"
  set RS=cn.execute(sql)
  InvRegNo = rs("REGPREFIX") & String(4-Len(CStr(rs("REGNUM"))),"0") & CStr(rs("REGNUM")) 
  set RS=nothing
  'sql = "UPDATE EXJRNLPREF SET REGNUM=REGNUM+1"
  'cn.execute(sql)

  sql = "INSERT INTO EXIREG (REGNUM,INVNUM,REFORDER,VENDORID,VENDORBRANCHID,INVRECDATE,INVDUEDATE,EMPCODE,DEPARTMENT,PONUM,LOCATION,"
  sql = sql & "INVCURRENCY,CURRENCYSYMBOL, INVAMOUNT,COMPCODE,VATPAIDTOVENDOR,WHTNAME,WHTRATE,VATRATE,WHTAMOUNT"
  if (request("RefOrder")="N") then
  sql = sql & ",DOC1, DOC2"
  end if
  sql = sql & ") VALUES ('"
  sql = sql & InvRegNo & "','"
  sql = sql & request("reqInvoiceNo") & "','"
  if (request("RefOrder")="Y") then
  sql = sql &  "Y','"
  end if
  if (request("RefOrder")="N") then 
  sql = sql &  "N','"
  end if
  if (request("RefOrder")="O") then 
  sql = sql &  "O','"
  end if
  sql = sql & request("reqVendorID") & "','"
  sql = sql & request("reqBranch") & "','"
  sql = sql & request("ReceivedDate") & "','"
  sql = sql & request("DueDate") & "','"
  sql = sql & request("reqReceivedBy") & "','"
  sql = sql & request("Dept") & "','"
  sql = sql & request("reqPONum") & "','"
  sql = sql  & "FINANCE','"
  sql = sql & request("reqCurrency") & "','"
  sql = sql & request("CurSymbol2") & "',"
  if (request("InvoiceAmount")<>"") then
  sql = sql & delkoma(request("InvoiceAmount")) & ",'"
  else
  sql = sql &  "0,'"
  end if
  sql = sql & request("CompCode") & "','"
  if (request("cbVAT")="Y") then
  sql = sql & "Y','"
  else
  sql = sql & "N','"
  end if
  sql = sql & request("WHTName") & "',"
  if (request("WHTRate")<>"") then
  sql = sql & request("WHTRate") & ","
  else
  sql = sql & "0,"
  end if
  sql = sql & request("VATRate") & ","
  if (request("WHTAmt") <> "") then
  sql = sql & delkoma(request("WHTAmt")) 
  else
  sql = sql & "0" 
  end if
  if (request("RefOrder")="N") then
    if (request("cbOri")="Y") then
  	sql = sql & ",'Y'"
  	else
  	sql = sql & ",'N'"
  	end if
  	if (request("cbTax")="Y") then
  	sql = sql & ",'Y'"
  	else
  	sql = sql & ",'N'"
  	end if
  end if
  sql = sql & ")" 
  cn.execute(sql)
  
  sql = "UPDATE EXJRNLPREF SET REGNUM=REGNUM+1"
  cn.execute(sql)
else
  sql = "UPDATE EXIREG SET "
  sql = sql & "WHTNAME='" & request("txttaxcode") & "',"
  sql = sql & "FACTNO='" & request("txtfactno") & "',"
  sql = sql & "FACTDATE='" & request("txtfactdate") & "'"
  sql = sql & " WHERE SEQNUM =" & request("seqnum2") 
  cn.execute(sql)


  cn.execute("DELETE [JKT-LLTESTING].TAXSYS.DBO.DTTAXCHARGE WHERE seq_inv = '" & request("seqnum2") & "'")
	
  if request("txttaxcode1") <> "" then
  	sql = "INSERT INTO [JKT-LLTESTING].TAXSYS.DBO.DTTAXCHARGE ("
  	sql = sql & "seq_inv,tax_code,tax_rate,currency_id,dpporg_amount,tax_pct,tax_amount,tax_flag,created_date,order_id)"
  	sql = sql & "VALUES("
  	sql = sql & "'" & request("seqnum2") & "',"
  	sql = sql & "'" & request("txttaxcode1")  & "',"
  	sql = sql & request("taxRate1")  & ","
	sql = sql & "'" & request("curSymbol2") & "',"
  	sql = sql & request("txtInvAmount") & ","
  	sql = sql & request("WHTRate1") & ","
  	sql = sql & request("txtTaxAmount1") & ","
  	sql = sql & "'" & request("txtType1") & "',"
  	sql = sql & "getDate(),1"
  	sql = sql & ")"
   	'response.write sql
	'response.end
	cn.execute(sql)
   end if
   
   if request("txttaxcode2") <> "" then
  	sql = "INSERT INTO [JKT-LLTESTING].TAXSYS.DBO.DTTAXCHARGE ("
  	sql = sql & "seq_inv,tax_code,tax_rate,currency_id,dpporg_amount,tax_pct,tax_amount,tax_flag,created_date,order_id)"
  	sql = sql & "VALUES("
  	sql = sql & "'" & request("seqnum2") & "',"
  	sql = sql & "'" & request("txttaxcode2")  & "',"
  	sql = sql & request("taxRate2")  & ","
	sql = sql & "'" & request("curSymbol2") & "',"
  	sql = sql & request("txtInvAmount") & ","
  	sql = sql & request("WHTRate2") & ","
  	sql = sql & request("txtTaxAmount2") & ","
  	sql = sql & "'" & request("txtType2") & "',"
  	sql = sql & "getDate(),2"
  	sql = sql & ")"
   	cn.execute(sql)
   end if
   
end if
'cn.execute(sql)

'update exicharge
'if (request("RefOrder") ="Y") then
'    For i = 1 to request("Incr")-1
'	if (request("aseqnum"&i) <> "0") then
'	  sql = "UPDATE EXICHARGE SET "
'	  sql = sql & "ACCTCODE='" & request("ActCode"&i) & "',"
'	  sql = sql & "T0CODE='" & request("T0"&i) & "',"
'	  sql = sql & "T1CODE='" & request("T1"&i) & "',"
'	  sql = sql & "T2CODE='" & request("T2"&i) & "',"
'	  sql = sql & "T3CODE='" & request("T3"&i) & "',"
'	  sql = sql & "AMOUNT=" & delkoma(request("Amt"&i)) & " "
'	  sql = sql & " WHERE SEQNUM=" & request("aseqnum"&i)
'	  cn.execute(sql)
'	else
'	  if (request("ActCode"&i) <> "") then
'	    sql = "INSERT INTO EXICHARGE(INVSEQNUM,ACCTCODE,T0CODE,"
'	  	sql = sql & "T1CODE, T2CODE, T3CODE, AMOUNT) VALUES ("
'	  	sql = sql & request("seqnum2") & ",'"
'	  	sql = sql & request("ActCode"&i) & "','"
'	  	sql = sql & request("T0"&i) & "','"
'	  	sql = sql & request("T1"&i) & "','"
'	  	sql = sql & request("T2"&i) & "','"
'	  	sql = sql & request("T3"&i) & "',"
'	  	sql = sql & delkoma(request("Amt"&i)) & ")"
'	  cn.execute(sql)
'	  end if
'	end if
	
'	Next
'end if

'Update Receipt
For i = 1 to CInt(request("Incr3"))-1
	  if (request("cb"&i)="Y")and(request("isEdit"&i)="Y") then
	    sql = "INSERT INTO EXILINKRCP (SEQNUM, PONUM, RECEIVERNUM)"
	    sql = sql & " VALUES ("
	    sql = sql & request("seqnum2") & ",'"
	    sql = sql & request("reqPONUM") & "','"
	    sql = sql & request("rn"&i) & "')"
	    cn.execute(sql)

	    sql = "EXEC dbo.EXIPORECUPDATE "
	  	sql = sql & "@InvoiceStatus='" & request("status") & "',"
	  	sql = sql & "@InvoiceNum='" & request("reqInvoiceNo") & "',"
	  	sql = sql & "@InvoiceDate='" & request("ReceivedDate") & "',"
	  	sql = sql & "@PONumber='" & request("reqPONum") & "',"
	  	sql = sql & "@ReceiverNum='" & request("rn"&i) & "'"
	  
	  	cn.execute(sql)
		isUpdate="Y"
	  End if
	Next
	
	if (isUpdate="Y") then
	sql = "UPDATE EXIREG SET INVSTATUS='APPROVED',"
	sql = sql & "SERVICERCP='Y', "
	sql = sql & "LOCATION='FINANCE' "
	sql = sql & " WHERE SEQNUM=" & request("seqnum2")
	cn.execute(sql)
	
	
	
	sql = "INSERT INTO EXIHIST (SEQNUM,TRANSTYPE,LOCATION,DEPARTMENT,DATE,UPDATEUSER,UPDATESTAMP)"
  	sql = sql & " VALUES ("
  	sql = sql & request("seqnum2") & ",'"
	sql = sql & "IUR','"
 	sql = sql & "FINANCE','"
  	sql = sql & request("Dept") & "','"
  	sql = sql & request("RouteDate") & "','"
  	sql = sql & usrName & "',"
  	sql = sql & "GETDATE() )"
    cn.execute(sql)	
	end if
'end of update receipt

dim tmpNum
if (request("seqnum2") = "0") then
  sql = "SELECT MAX(SEQNUM) FROM EXIREG"
  set rs=cn.execute(sql)
  tmpNum=rs(0)
  sql = "INSERT INTO EXIHIST (SEQNUM, TRANSTYPE,LOCATION,DEPARTMENT,DATE,UPDATEUSER,UPDATESTAMP)"
  sql = sql & " VALUES ("
  sql = sql & tmpNum & ",'"
  sql = sql & "IRC','"
  sql = sql & request("reqReceivedBy") & "','"
  sql = sql & request("Dept") & "','"
  sql = sql & request("ReceivedDate") & "','"
  sql = sql & usrName & "',"
  sql = sql & "GETDATE() )"
  
  cn.execute(sql)
  
  response.redirect "inv_Print.asp?seqnum=" &tmpNum
 
else
 tmpNum = request("seqnum2")
 'response.redirect "Inv_Detail.asp?seqnum=" & tmpNum
 response.redirect "Inv_DetailTax.asp?seqnum="& tmpNum &"&InvStatus="& request("InvStatus")&"&dsort="& request("dsort")&"&dorder="&request("dorder") &"&Location="&request("Location")&"&UserCoord="&request("UserCoord")&"&RefOrder="&request("RefOrder2")&"&VendorID="&request("VendorID")&"&CurSymbol="& request("CurSymbol")
 
end if 
%>
